package com.app.conf;


import com.alibaba.fastjson.JSONObject;
import com.app.controller.BaseController;
import com.app.util.Result;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 拦截器
 */
public class Interceptor extends BaseController implements HandlerInterceptor {

//    @Autowired
//    private RedisUtil redisUtil;

    /**
     * 在请求处理之前进行调用（Controller方法调用之前）
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        try {
            //判断是否登录
            boolean verifyPermissions = verifyPermissions(request);
//            //判断是否有权限
//            boolean competence = competence(request);
//            if (verifyPermissions && competence) {
//                return true;
//            }
            //这里设置拦截以后重定向的页面，一般设置为登陆页面地址
//           String msg = JSONObject.toJSONString(Result.error(500,"无权限"));
//           returnJson(response,msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;//如果设置为false时，被请求时，拦截器执行到此处将不会继续操作
        //如果设置为true时，请求将会继续执行后面的操作
    }

    /*返回客户端数据*/
//    private void returnJson(HttpServletResponse response, String json) throws Exception{
//        PrintWriter writer = null;
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html; charset=utf-8");
//        try {
//            writer = response.getWriter();
//            writer.print(json);
//
//        } catch (IOException e) {
//        } finally {
//            if (writer != null)
//                writer.close();
//        }
//    }



}

